package com.kaili.basic.controlle;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.BaseSuggestionAddDto;
import com.kaili.basic.pojo.dto.BaseSuggestionQueryDto;
import com.kaili.basic.pojo.entity.BaseSuggestion;
import com.kaili.basic.service.BaseSuggestionService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.tenant.DbContextHolder;
import com.kaili.common.util.CommonConstant;
import com.kaili.common.util.LoginDetailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Created by renzhikun on 18-10-10.
 */
@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Api(tags = "意见建议", description = "意见建议相关API")
public class BaseSuggestionController {

    @Autowired
    private BaseSuggestionService suggestionService;

    @PostMapping("/suggestions")
    @ApiOperation(value = "意见登记", notes = "意见登记")
    public RestStatus addSupply(@Validated @RequestBody BaseSuggestionAddDto dto) {
        BaseSuggestion baseSuggestion = new BaseSuggestion();
        BeanUtils.copyProperties(dto,baseSuggestion);
        baseSuggestion.setCompanyId(Integer.parseInt(LoginDetailUtils.getCompanyCode()));
        baseSuggestion.setSiteId(LoginDetailUtils.getSiteId());

        String currentDbType = DbContextHolder.getDbType();
        DbContextHolder.setDbType(CommonConstant.MAIN_DB_WRITE);

        String result = suggestionService.getMaxSuggestionCode();
        if (StringUtils.isEmpty(result)) {
            result = "0001";
        }else {
            Integer code = Integer.valueOf(result) + 1;
            switch (code.toString().length()) {
                case 1 :
                    result = "000" + code.toString();
                    break;
                case 2 :
                    result = "00" + code.toString();
                    break;
                case 3 :
                    result = "0" + code.toString();
                    break;
                case 4 :
                    result = code.toString();
                    break;
                default :
                    result = "-1";
                    break;
            }
        }

        baseSuggestion.setSuggestionCode(result);
        suggestionService.addBaseSuggestion(baseSuggestion);

        DbContextHolder.setDbType(currentDbType);
        return CommonStatus.ADD_OK;
    }

    @GetMapping("/suggestions")
    @ApiOperation(value = "意见查询", notes = "意见查询")
    public Page<BaseSuggestion> querySuggestion(@ModelAttribute BaseSuggestionQueryDto dto) {
        return suggestionService.queryBaseSuggestion(new Page<BaseSuggestion>(dto.getPage(), dto.getPageSize()), dto);
    }
}
